home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / C005.ZIP / SINE.C < prev    next >
Text File  |  1990-01-19  |  2KB  |  82 lines

  1. /********************************************************************
  2.  * C Users Group (U.K) C Source Code Library File CUGLIB.005        *
  3.  * Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd.         *
  4.  * Edgbaston, Birmingham B15 2QN ENGLAND                *
  5.  ********************************************************************
  6.  * File name: sine.c
  7.  * Program name: library modules only
  8.  * Source of file: The Public Domain Software Library.
  9.  * Purpose: maths function
  10.  * Changes: <who what when & why major changes have been made>      
  11.  ********************************************************************/
  12.  
  13. /***********************************************************
  14.  *               The TULSA IBM C BOARD                     *
  15.  *                   918-664-8737                          *
  16.  *             300/1200 XMODEM, 24 Hours                   *
  17.  **********************************************************/
  18.  
  19.  
  20. #include "math.h"
  21. #include "errno.h"
  22.  
  23. double cos(x)
  24. double x;
  25. {
  26.         double sincos();
  27.  
  28.         return sincos(x, fabs(x) + 1.57079632679489661923, 0);
  29. }
  30.  
  31. double sin(x)
  32. double x;
  33. {
  34.         double sincos();
  35.  
  36.         if (x < 0.0)
  37.                 return sincos(x,-x,1);
  38.         else
  39.                 return sincos(x,x,0);
  40. }
  41.  
  42. #define R1 -0.16666666666666665052e+00
  43. #define R2 +0.83333333333331650314e-02
  44. #define R3 -0.19841269841201840457e-03
  45. #define R4 +0.27557319210152756119e-05
  46. #define R5 -0.25052106798274584544e-07
  47. #define R6 +0.16058936490371589114e-09
  48. #define R7 -0.76429178068910467734e-12
  49. #define R8 +0.27204790957888846175e-14
  50.  
  51. #define YMAX 6.7465e09
  52.  
  53. static double sincos(x,y,sgn)
  54. double x,y;
  55. {
  56.         double f, xn, r, g;
  57.         extern int errno;
  58.  
  59.         if (y >= YMAX) {
  60.                 errno = ERANGE;
  61.                 return 0.0;
  62.         }
  63.         if (modf(y * 0.31830988618379067154, &xn) >= 0.5)
  64.                 ++xn;
  65.         if ((int)xn & 1)
  66.                 sgn = !sgn;
  67.         if (fabs(x) != y)
  68.                 xn -= 0.5;
  69.         g = modf(fabs(x), &x);          /* break into fraction and integer parts */
  70.         f = ((x - xn*3.1416015625) + g) + xn*8.9089102067615373566e-6;
  71.         if (fabs(f) > 2.3283e-10) {
  72.                 g = f*f;
  73.                 r = (((((((R8*g R7)*g R6)*g R5)*g
  74.                                 R4)*g R3)*g R2)*g R1)*g;
  75.                 f += f*r;
  76.         }
  77.         if (sgn)
  78.                 f = -f;
  79.         return f;
  80. }
  81.  
  82.